/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrweb.manager;
/**
 * PackingToFlexHelper
 * 
 * @author biggie
 * @version $Revision: 1.1 $ $Date: 2008/07/02 02:11:02 $
 */
public class PackingToFlexHelper {

	private StringBuffer sb  = new StringBuffer();
	
	public final String ERROR_CODE_TAG = "error_code";
	
	public final String ERROR_MES_TAG = "error_message";
	
	public final String RESULT_SIZE = "result_size";
	
	public final String RESULT_DATA = "result_data";
	
	public final String IS_ERROR = "is_error";// return yes or no 
	
	
	public void setErrorCode(String errorCode){
		removeTag(ERROR_CODE_TAG);
		sb.append("<"+ERROR_CODE_TAG+">");
		sb.append(errorCode);
		sb.append("</"+ERROR_CODE_TAG+">");
	}
	public void setErrorMessage(String errorMessage){
		removeTag(ERROR_MES_TAG);
		sb.append("<"+ERROR_MES_TAG+">");
		sb.append(errorMessage);
		sb.append("</"+ERROR_MES_TAG+">");
	}
	public void setResultSize(String resultSize){
		removeTag(RESULT_SIZE);
		sb.append("<"+RESULT_SIZE+">");
		sb.append(resultSize);
		sb.append("</"+RESULT_SIZE+">");
	}	
	public void setResultData(String resultData){
		removeTag(RESULT_DATA);
		sb.append("<"+RESULT_DATA+">");
		sb.append(resultData);
		sb.append("</"+RESULT_DATA+">");
	}	
	public void setIsError(boolean isError){
		removeTag(IS_ERROR);
		sb.append("<"+IS_ERROR+">");
		sb.append(String.valueOf(isError));
		sb.append("</"+IS_ERROR+">");
	}	
	private String getTag(String tagStr){
		String srcStr = sb.toString();
		if(srcStr.indexOf("<"+tagStr+">") == -1){
			return "";
		}
		int startIndex = srcStr.indexOf("<"+tagStr+">") + tagStr.length() + 2;
		int endIndex = srcStr.indexOf("</"+tagStr+">");
		String strDatas = srcStr.substring(startIndex,endIndex);
		return strDatas;
	}
	
	private void removeTag(String tagStr){
		String strTag = this.getTag(tagStr);
		if(strTag.length() == 0){
			return;
		}
		sb = new StringBuffer(sb.toString().replaceAll(strTag, ""));
	}
	
	public String getData(){
		
		String errorCode = this.getTag(ERROR_CODE_TAG);
		if (errorCode.length() == 0)
			this.setErrorCode("");
		String errorMessage = this.getTag(ERROR_MES_TAG);
		if (errorMessage.length() == 0)
			this.setErrorMessage("");
		String resultSize = this.getTag(RESULT_SIZE);
		if (resultSize.length() == 0)
			this.setResultSize("0");
		String resultData = this.getTag(RESULT_DATA);
		if (resultData.length() == 0)
			this.setResultData("");
		String isError = this.getTag(IS_ERROR);
		if (isError.length() == 0)
			this.setIsError(false);
		
		return sb.toString();
	}
	public String toString(){
		return this.getData();
	}
	
}
